package cn.car91.pager;

import cn.car91.utils.Car91JsonUtil;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

/**
 * 分页通讯
 * 文件 cn.car91.utils.HttpPager.java
 *
 * 分页获取数据，如果接收到数据，分页+1，反之不变
 * 服务端通过“p”获取当前页
 *
 * @author 周海刚
 *         时间 2014年12月9日
 */
public class HttpPager {
    private int page = 1;
    private RequestCallBack callBack;

    public RequestCallBack getCallBack() {
		return callBackPager;
	}
    
	/**
     * 分页复位，即当前分页置为1
     */
    public void reset() {
        page = 1;
    }

    public void setCallBack(RequestCallBack callBack) {
		this.callBack = callBack;
	}

	public int getPage() {
		return page;
	}

	RequestCallBack callBackPager = new RequestCallBack() {

        @Override
        public Object getUserTag() {
            // TODO Auto-generated method stub
            return callBack.getUserTag();
        }

        @Override
        public void onCancelled() {
            // TODO Auto-generated method stub
            callBack.onCancelled();
        }

        @Override
        public void onLoading(long total, long current, boolean isUploading) {
            // TODO Auto-generated method stub
            callBack.onLoading(total, current, isUploading);
        }

        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            callBack.onStart();
        }

        @Override
        public void setUserTag(Object userTag) {
            // TODO Auto-generated method stub
            callBack.setUserTag(userTag);
        }

        @Override
        public void onFailure(HttpException arg0, String arg1) {
            // TODO Auto-generated method stub
            callBack.onFailure(arg0, arg1);
        }

        @Override
        public void onSuccess(ResponseInfo arg0) {
            // TODO Auto-generated method stub
            if (Car91JsonUtil.getDTOList(arg0.result.toString(), Object.class).size() > 0)
                page++;

            callBack.onSuccess(arg0);
        }
    };
}
